﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Relations.DirectAssociations
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class DbEntityRelationDiscoveryInstruction:InstructionBase
    {
        #region props
        private DataProviderType _DbProvider;
        /// <summary></summary>
        public DataProviderType DbProvider
        {
            get
            {
                return this._DbProvider;
            }
            set
            {
                this._DbProvider = value;
            }
        }

        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private List<string> _Databases;
        /// <summary></summary>
        public List<string> Databases
        {
            get
            {
                return this._Databases;
            }
            set
            {
                this._Databases = value;
            }
        }

        private bool _InferTableRelations;
        /// <summary></summary>
        public bool InferTableRelations
        {
            get
            {
                return this._InferTableRelations;
            }
            set
            {
                this._InferTableRelations = value;
            }
        }

        private string _ResultFilePath;
        /// <summary></summary>
        public string ResultFilePath
        {
            get
            {
                return this._ResultFilePath;
            }
            set
            {
                this._ResultFilePath = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public DbEntityRelationDiscoveryInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._DbProvider = DataProviderType.MSSQL;
            this._ConnStr = string.Empty;
            this._Databases=new List<string>();
            this._InferTableRelations = false;
            this._ResultFilePath = string.Empty;
        }

        /// <summary>
        /// constructor, all derived class must call default constructor to initialize
        /// default settings.
        /// </summary>
        public DbEntityRelationDiscoveryInstruction(
            Guid componentID, Guid jobTicketID,
            DataProviderType dbProvider, string connStr, 
            bool inferRelations, List<string> Dbs, string resultFilePath) 
            : base(componentID, jobTicketID)
        {
            this._DbProvider = dbProvider;
            this._ConnStr = connStr;
            this._InferTableRelations = inferRelations;
            this._Databases = Dbs;
            this._ResultFilePath = resultFilePath;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DbProvider",this.DbProvider.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr",this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "InferRelations",this.InferTableRelations.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DbNames", StringUtil.ToString(this.Databases.ToArray()));
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ResultFilePath", this.ResultFilePath);
            
            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                DbEntityRelationDiscoveryInstruction instruction = new DbEntityRelationDiscoveryInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.DbProvider = (DataProviderType) Enum.Parse(
                                                                typeof (DataProviderType),
                                                                XmlDataUtil.GetAttributeValue(
                                                                    xNode, "DbProvider",
                                                                    DataProviderType.MSSQL.ToString()));
                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.InferTableRelations =
                    bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "InferRelations", false.ToString()));
                string[] Dbs = StringUtil.Split(XmlDataUtil.GetAttributeValue(xNode, "DbNames", ""));

                instruction.Databases=new List<string>();
                instruction.Databases.AddRange(Dbs);
                instruction.ResultFilePath = XmlDataUtil.GetAttributeValue(xNode, "ResultFilePath", "");

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
